package uk.gov.swmprobation.cplite;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.view.View;
import android.widget.CheckBox;

public class Helper {

	public static AlertDialog dialog;
	
	@SuppressWarnings("deprecation")
	public static boolean checkWifi(){
		ConnectivityManager connManager = (ConnectivityManager) MyApplication.getContext().getSystemService(android.content.Context.CONNECTIVITY_SERVICE);
		NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (mWifi.isConnected()) {;
		    return connManager.getBackgroundDataSetting();
		}
		return false;
	}
	public static String shortName(String longName){
		String shortName = longName.split(",")[0];
		return shortName;
	}
	
	public static void showHintDialog(final Context context,final String title,String message,int icon){
		View checkBoxView = View.inflate(context, R.layout.save_setting, null);
		final CheckBox checkBox = (CheckBox) checkBoxView.findViewById(R.id.savesettingcheck);
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(message);
	    builder.setTitle(title);
	    builder.setIcon(icon);
	    builder.setView(checkBoxView);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   if (checkBox.isChecked()){
		        		   SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
		        		   sharedPref.edit().putBoolean(title, false).commit();
		        	   }
		           }
		       });

		dialog = builder.create();
		dialog.show();
	}
	
	@SuppressWarnings("deprecation")
	@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
	public static boolean isAirplaneModeOn(Context context){

	    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
	        return Settings.System.getInt(context.getContentResolver(), 
	                Settings.System.AIRPLANE_MODE_ON, 0) != 0;          
	    } else {
	        return Settings.Global.getInt(context.getContentResolver(), 
	                Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
	    }       
	}
	
	public static boolean isNetworkAvailable(Context context) {
	    ConnectivityManager connectivityManager 
	          = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
	    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
	}
	
}
